import QtQuick 2.0
import "."

QtObject {
    id: id_root
    property string modelname: "RabbitmqModel"
    property string servicename: Utils._SERVICE_NAME_STORAGE
    property var filterFunc: (function () {
        //true skip/ignore the msg
        //false update msg
        return false
    })
    function compare2Objects(x, y) {
        // If both x and y are null or undefined and exactly the same
        if (x === y) {
            return true
        }

        // If they are not strictly equal, they both need to be Objects
        if (!(x instanceof Object) || !(y instanceof Object)) {
            return false
        }

        //They must have the exact same prototype chain,the closest we can do is
        //test the constructor.
        if (x.constructor !== y.constructor) {
            return false
        }
        for (var p in x) {
            //Inherited properties were tested using x.constructor === y.constructor
            if (x.hasOwnProperty(p)) {
                // Allows comparing x[ p ] and y[ p ] when set to undefined
                if (!y.hasOwnProperty(p)) {
                    return false
                }

                // If they have the same strict value or identity then they are equal
                if (x[p] === y[p]) {
                    continue
                }

                // Numbers, Strings, Functions, Booleans must be strictly equal
                if (typeof (x[p]) !== "object") {
                    return false
                }
            }
        }

        for (p in y) {
            // allows x[ p ] to be set to undefined
            if (y.hasOwnProperty(p) && !x.hasOwnProperty(p)) {
                return false
            }
        }
        return true
    }

    function update(msg) {
        var jMsg
        try {
            if (undefined === msg) {
                return
            } else if (typeof msg == "string") {
                jMsg = JSON.parse(msg)
            } else if (typeof msg == "json") {
                jMsg = JSON.parse(JSON.stringify(msg))
            }
            //ignore some message by filterFunc and check_resource
            if (typeof id_root.filterFunc == "function") {
                if (id_root.filterFunc(jMsg))
                    return
            }
            if (!check_resource(jMsg)) {
                return
            }
            if (jMsg.hasOwnProperty("data")) {
                jMsg = JSON.parse(JSON.stringify(jMsg.data))
            }
            for (var k in jMsg) {
                if (id_root.hasOwnProperty(k)) {
                    var obj = JSON.parse(JSON.stringify(id_root[k]))
                    obj.value = jMsg[k]
                    if (!compare2Objects(obj.value, id_root[k].value)) {
                        id_root[k] = obj
                    }
                } else {
                    console.log(id_root.modelname + " doesn't have property: ",
                                k)
                }
            }
        } catch (e) {
            console.warn(id_root.modelname + "update failed: " + e + " msg:" + msg)
        }
    }

    function save() {
        var convertedString = JSON.stringify(id_root)
        console.log("converted string :", convertedString)
    }

    function check_resource(jmsg) {
        if (id_root.hasOwnProperty("resource") && jmsg.hasOwnProperty(
                    "resource")) {
            return id_root.resource === jmsg.resource
        } else {
            return true
        }
    }

    Component.onCompleted: {
        if (servicename.length > 0) {
            RabbitmqManager.subscribe(servicename, function (msg) {
                id_root.update(msg)
            })
        } else {
            console.warn(id_root.modelname + " please check model servicename!!!")
        }
    }
}
